package it.gtd.swimpool.model.entity;

import com.google.common.annotations.Beta;

import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.Date;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

@Entity
@Table(name = "messages")
@Beta
public class Message {
    private String text;

    @EmbeddedId
    private MessageId composed;

    public Message() {
    }

    public Message(String text, User sender, User receiver) {
        this();

        checkNotNull(text, "text can't be null");
        checkNotNull(sender, "sender can't be null");
        checkNotNull(receiver, "receiver can't be null");

        this.setText(text);
        this.composed = this.composed == null ? new MessageId() : this.composed;
        this.composed.setSender(sender);
        this.composed.setReceiver(receiver);
    }

    private void setText(String text) {
        checkNotNull(text, "text can't be null");
        checkArgument(!text.isEmpty(), "Message text can't be empty!");

        this.text = text;
    }

    public String getText() {
        return this.text;
    }

    public User getSender() {
        return this.composed.getSender();
    }

    public User getReceiver() {
        return this.composed.getReceiver();
    }

    public Date getDate() {
        return this.composed.getDate();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Message message = (Message) o;

        if (!composed.equals(message.composed)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return composed.hashCode();
    }
}

